package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserInfoApiImpl implements UserInfoApi{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public Page<UserInfo> findByIds(List<Long> ids, RecommendUserDto dto) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);
        if(dto!=null){
            queryWrapper.eq(StringUtils.hasText(dto.getGender()),UserInfo::getGender,dto.getGender());
            queryWrapper.le(dto.getAge()!=null,UserInfo::getAge,dto.getAge());
        }
        Page<UserInfo> page = new Page<>(dto.getPage(), dto.getPagesize());
        return userInfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<UserInfo> findByUserIds(List<Long> ids) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class).in(UserInfo::getId,ids));
    }

    @Override
    public Page<UserInfo> findMapByIds(List<Long> friendIds, Integer page, Integer pagesize, String keyword) {
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UserInfo::getId,friendIds);
        lambdaQueryWrapper.like(StringUtils.hasLength(keyword), UserInfo::getNickname,"%"+keyword+"%");
        Page<UserInfo> page1 = new Page<>(page, pagesize);
        Page<UserInfo> pages = userInfoMapper.selectPage(page1, lambdaQueryWrapper);
        return pages;
    }

    @Override
    public List<UserInfo> findByUserIds(List<Long> userIds, String gender) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                                    .in(CollUtil.isNotEmpty(userIds), UserInfo::getId,userIds)
                                    .eq(StringUtils.hasLength(gender),UserInfo::getGender,gender));
    }

    @Override
    public Page<UserInfo> findAll(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<>(page,pagesize),null);
    }

    @Override
    public void levelUp(Long userId) {
        UserInfo userInfo = findById(userId);
        Integer level = userInfo.getLevel();
        if(level==null){
            userInfo.setLevel(2);
        }else if(level <3){
            userInfo.setLevel(level +1);
        }
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public List<UserInfo> findByNicknameIds(List<Long> ids, String nickname) {
        List<UserInfo> list = new ArrayList<>();
        if(CollUtil.isNotEmpty(ids)&&ids.size() > 0){
            list = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                    .in(UserInfo::getId,ids)
                    .like(!StringUtils.isEmpty(nickname),UserInfo::getNickname,nickname));
        }
        return list;
    }
}
